/**
 *
 *  License: http://www.apache.org/licenses/LICENSE-2.0
 *  Home page: https://gitee.com/linlurui/service-abc
 *  Note: to build on java, include the jdk1.8+ compiler symbol (and yes,
 *  I know the difference between language and runtime versions; this is a compromise).
 * @author linlurui
 * @Date Date: 2019-02-10
 */


package abc.utils;

import org.jcodec.api.FrameGrab;
import org.jcodec.api.JCodecException;
import org.jcodec.common.model.Picture;
import org.jcodec.scale.AWTUtil;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public final class VideoUtil {

    private static final int THUMB_FRAME = 5;

    private VideoUtil() { }

    /**
     * 获取视频指定帧
     */
    public static BufferedImage getFrame (File file)
            throws IOException, JCodecException {

        return getFrame(file, THUMB_FRAME);
    }

    /**
     * 获取视频指定帧
     */
    public static BufferedImage getFrame (File file, int frameNumber)
            throws IOException, JCodecException {

        Picture picture = FrameGrab.getFrameFromFile(file, frameNumber);

        BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture);

        return bufferedImage;
    }

    /**
     * 获取缩略图
     * @return
     */
    public static byte[] getThumbnail(File file) throws IOException, JCodecException {

        return getThumbnail(file, 33);
    }

    /**
     * 获取缩略图
     * @return
     */
    public static byte[] getThumbnail(File file, int scalRatio) throws IOException, JCodecException {

        BufferedImage frameBi = getFrame(file);

        frameBi = ImageUtil.zoomImage(frameBi, scalRatio);

        return ImageUtil.toBytes(frameBi);
    }
}
